//package com.yunxin.admin.system.api.statistics;
//import com.yunxin.admin.system.infra.common.constant.SecurityConstants;
//import com.yunxin.admin.system.infra.framework.resubmit.Resubmit;
//import com.yunxin.admin.system.infra.framework.security.util.SecurityUtils;
//import com.yunxin.admin.system.infra.framework.syslog.AutoLog;
//import com.yunxin.common.result.PageResult;
//import com.yunxin.common.result.Result;
//import com.yunxin.core.application.dto.channel.ChannelPromotionChargedInfoDTO;
//import com.yunxin.core.application.dto.merchant.MerchantListDTO;
//import com.yunxin.core.application.dto.product.ProductChargedInfoDTO;
//import com.yunxin.core.application.service.ChannelQueryAdminService;
//import com.yunxin.core.application.service.MerchantQueryAdminService;
//import com.yunxin.core.application.service.ProductQueryAdminService;
//import com.yunxin.core.infra.persistence.entity.channel.ChannelProhibitProduct;
//import com.yunxin.core.infra.persistence.mapper.channel.ChannelProhibitProductMapper;
//import com.yunxin.statistics.application.command.ChannelPromotionTransformPageCommand;
//import com.yunxin.statistics.application.command.ProductTransformPageCommand;
//import com.yunxin.statistics.application.command.UpdateChannelPromotionTransformChannelUvCommand;
//import com.yunxin.statistics.application.dto.ChannelPromotionTransformPageDTO;
//import com.yunxin.statistics.application.dto.ProductTransformPageDTO;
//import com.yunxin.statistics.application.service.StatisticsApplicationAdminService;
//import com.yunxin.statistics.application.service.StatisticsQueryAdminService;
//import io.swagger.v3.oas.annotations.Operation;
//import io.swagger.v3.oas.annotations.security.SecurityRequirement;
//import io.swagger.v3.oas.annotations.tags.Tag;
//import lombok.RequiredArgsConstructor;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.security.access.prepost.PreAuthorize;
//import org.springframework.validation.annotation.Validated;
//import org.springframework.web.bind.annotation.PostMapping;
//import org.springframework.web.bind.annotation.RequestBody;
//import org.springframework.web.bind.annotation.RequestMapping;
//import org.springframework.web.bind.annotation.RestController;
//import com.yunxin.openapi.receive.common.domain.service.CallbackStatisticsService;
//import java.math.BigDecimal;
//import java.math.RoundingMode;
//import java.util.*;
//import java.util.function.Function;
//import java.util.stream.Collectors;
//
///**
// * @author zero°
// * @since 2023-05-29
// */
//@Tag(name = "91.统计管理")
//@Slf4j
//@RestController
//@RequestMapping("/core/v1")
//@RequiredArgsConstructor
//public class StatisticsController {
//
//    private final StatisticsApplicationAdminService statisticsApplicationAdminService;
//
//    private final StatisticsQueryAdminService statisticsQueryAdminService;
//
//    private final MerchantQueryAdminService merchantQueryAdminService;
//
//    private final ProductQueryAdminService productQueryAdminService;
//
//    private final ChannelQueryAdminService channelQueryAdminService;
//
//    private final CallbackStatisticsService callbackStatisticsService;
//
//    @Operation(summary = "更新渠道uv", security = {@SecurityRequirement(name = SecurityConstants.TOKEN_KEY)})
//    @PostMapping("/channel/transform/updateChannelUv")
//    @Resubmit
//    @PreAuthorize("@ss.hasPerm('core:channel_promotion_transform:edit')")
//    @AutoLog(value = "更新渠道uv")
//    public Result<Boolean> updateChannelUv(@Validated @RequestBody UpdateChannelPromotionTransformChannelUvCommand command) {
//        command.setEditor(SecurityUtils.X.getUser().getUsername());
//        return Result.success(statisticsApplicationAdminService.updateChannelUv(command));
//    }
//
//    @Operation(summary = "分页查询产品转化统计列表", security = {@SecurityRequirement(name = SecurityConstants.TOKEN_KEY)})
//    @PostMapping("/product/transformStatisticsPageQuery")
//    public PageResult<ProductTransformPageDTO> transformStatisticsPageQuery(@Validated @RequestBody ProductTransformPageCommand command) {
//        PageResult<ProductTransformPageDTO> result = statisticsQueryAdminService.productTransformStatisticsPageQuery(command);
//        List<ProductTransformPageDTO> results = result.getData().getList();
//        if (results == null || results.isEmpty()){
//            return result;
//        }
//        Set<Long> productIds = results.stream().map(ProductTransformPageDTO::getProductId).collect(Collectors.toSet());
//        Map<Long, ProductChargedInfoDTO> productMap = productQueryAdminService.listProductCharged(productIds).stream().collect(Collectors.toMap(ProductChargedInfoDTO::getProductId, Function.identity()));
//        Set<Long> merchantIds = results.stream().map(ProductTransformPageDTO::getMerchantId).collect(Collectors.toSet());
//        Map<Long, MerchantListDTO> merchantListResultMap = merchantQueryAdminService.listMerchantByIds(merchantIds).stream().collect(Collectors.toMap(MerchantListDTO::getId, Function.identity()));
//        results.forEach(x -> {
//            MerchantListDTO merchantList = merchantListResultMap.get(x.getMerchantId());
//            Optional.ofNullable(merchantList).ifPresent(m -> x.setMerchantName(m.getMerchantName()));
//            ProductChargedInfoDTO productChargedInfoDTO = productMap.get(x.getProductId());
//            Optional.ofNullable(productChargedInfoDTO).ifPresent(p -> {
//                x.setProductName(p.getProductName());
//                x.setChargedWay(p.getChargedWay());
//                x.setChargedWayStr(p.getChargedWayStr());
//            });
//            Long merchantId = x.getMerchantId();
//            Date statisticsDate = x.getStatisticsDate();
//            Long userApplyStatisticsCount = callbackStatisticsService.getUserApplyStatisticsCount(merchantId,statisticsDate);
//            x.setApplyCount(0L);
//            if(userApplyStatisticsCount!=0 ){
//                x.setApplyCount(userApplyStatisticsCount);
//            }
//        });
//        return PageResult.success(results, result.getData().getTotal());
//
//    }
//
//
//    @Operation(summary = "分页查询渠道转化列表", security = {@SecurityRequirement(name = SecurityConstants.TOKEN_KEY)})
//    @PostMapping("/channel/transform/channelPromotionTransformPageQuery")
//    public PageResult<ChannelPromotionTransformPageDTO> channelPromotionTransformPageQuery(@Validated @RequestBody ChannelPromotionTransformPageCommand command) {
//        PageResult<ChannelPromotionTransformPageDTO> result = statisticsQueryAdminService.channelPromotionTransformPageQuery(command);
//        List<ChannelPromotionTransformPageDTO> results = result.getData().getList();
//        if (results == null || results.isEmpty()){
//            return result;
//        }
//        Set<String> promotionCode = results.stream().map(ChannelPromotionTransformPageDTO::getChannelPromotionCode).collect(Collectors.toSet());
//        Map<String, ChannelPromotionChargedInfoDTO> channelPromtionMap = channelQueryAdminService.listChannelPromotionCharged(promotionCode).stream().collect(Collectors.toMap(ChannelPromotionChargedInfoDTO::getPromotionCode, Function.identity()));
//        results.forEach(x -> {
//            ChannelPromotionChargedInfoDTO chargedInfoDTO = channelPromtionMap.get(x.getChannelPromotionCode());
//            if (Objects.nonNull(chargedInfoDTO)) {
//                x.setChannelName(chargedInfoDTO.getChannelName());
//                x.setChargedWay(chargedInfoDTO.getChargedWay());
//                x.setChargedWayStr(chargedInfoDTO.getChargedWayStr());
//                x.setChannelPromotionName(chargedInfoDTO.getChannelPromotionName());
//            }
//            if (x.getSystemRegister() != 0) {
//                BigDecimal formTransform = BigDecimal.valueOf(x.getSystemFrom()).divide(BigDecimal.valueOf(x.getSystemRegister()), 2, RoundingMode.HALF_UP).multiply(BigDecimal.valueOf(100));
//                x.setFormTransform(formTransform + "%");
//            }
//            x.setFormDeal(x.getSystemFrom() - x.getSystemFromInferior());
//            if (x.getSystemFrom() != 0) {
//                BigDecimal formDealTransform = BigDecimal.valueOf(x.getFormDeal()).divide(BigDecimal.valueOf(x.getSystemFrom()), 2, RoundingMode.HALF_UP).multiply(BigDecimal.valueOf(100));
//                x.setFormDealTransform(formDealTransform + "%");
//            }
//            x.setSumIncome(x.getSystemFromIncome().add(x.getSystemOrderUvIncome()));
//            if (Objects.nonNull(x.getChannelUv())) {
//                if (x.getChannelUv() != 0) {
//                    BigDecimal uvTransform = BigDecimal.valueOf(x.getSystemUv()).divide(BigDecimal.valueOf(x.getChannelUv()), 2, RoundingMode.HALF_UP).multiply(BigDecimal.valueOf(100));
//                    x.setUvTransform(uvTransform + "%");
//                    BigDecimal registerTransform = BigDecimal.valueOf(x.getSystemRegister()).divide(BigDecimal.valueOf(x.getChannelUv()), 2, RoundingMode.HALF_UP).multiply(BigDecimal.valueOf(100));
//                    x.setRegisterTransform(registerTransform + "%");
//                }
//                if (Objects.nonNull(x.getChargedPrice()) && BigDecimal.ZERO.compareTo(x.getChargedPrice()) < 0){
//                    x.setCost(x.getChargedPrice().multiply(new BigDecimal(x.getChannelUv())));
//                    x.setRoi(x.getSumIncome().divide(x.getCost(), 2, RoundingMode.HALF_UP));
//                }
//            }
//        });
//        return PageResult.success(results, result.getData().getTotal());
//    }
//
//
//}
